package com.testapp.supers.app;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

import static android.content.Context.VIBRATOR_SERVICE;


public class BlankFragment extends Fragment {

    private static final String ARG_SHOW_TEXT = "text";

    private String mContentText;
    // listView
    private ListView  lv;
    private ArrayAdapter<String>  adapter;
    private List<String> list;
    public BlankFragment() {
    }

    private Button button2;
    private MediaPlayer mp;
    private Vibrator vibrator;
    private SweetAlertDialog sweetAlertDialog;

    public static BlankFragment newInstance(String param1) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_SHOW_TEXT, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mContentText = getArguments().getString(ARG_SHOW_TEXT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
        button2 = rootView.findViewById(R.id.button2);
        button2.setOnClickListener(listener);
        //准备警报声音并监听，实现循环播放
        mp = MediaPlayer.create(getContext(), R.raw.music);
        //准备振动
        vibrator = (Vibrator)getActivity().getSystemService(VIBRATOR_SERVICE);
        // 初始化适配器
        lv = rootView.findViewById(R.id.lv);
        list = new ArrayList<String>();
        List strs = new ArrayList<String>();
        strs.add("一");
        strs.add("二");
        strs.add("三");
        strs.add("四");
        strs.add("五");
        for (int i = 0 ; i < 5; i++) {
            list.add("监测站点" + strs.get(i));
        }
        adapter = new ArrayAdapter<String>(getActivity(),R.layout.firstitem,list);
        //android.R.layout.simple_list_item_1
        lv.setAdapter(adapter);

        /// 给listItem设置点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            //  4个参数
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getActivity(), "点击的是" + position, Toast.LENGTH_SHORT).show();
                /// 进入搜索结果页面
                Intent tent = new Intent(getActivity(), SearchresultActivity.class);
                startActivity(tent);
                getActivity().overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
            }
        });
//      TextView contentTv = rootView.findViewById(R.id.content_tv);
//      contentTv.setText(mContentText);
        return rootView;
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button2:
                    mp = MediaPlayer.create(getContext(), R.raw.music);
                    mp.start();
//                    vibrator.vibrate(10000);
                    long[] pattern = {0,1000,0,1000,0,1000,0,1000,0,1000,0,1000,0,1000};
                    vibrator.vibrate(pattern,0);
                    sweetAlertDialog = new SweetAlertDialog(getContext());
                    sweetAlertDialog.setTitleText("收到警报查看")
                            .setConfirmText("OK")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    mp.stop();
                                    vibrator.cancel();
                                    sweetAlertDialog.cancel();
                                }
                            })
                            .show();
                    break;
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
    }
}
